package com.quanyue.music.order.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.RequestContext;
import com.quanyue.auto.web.Scope;
import com.quanyue.auto.web.WebUtils;
import com.quanyue.music.domain.constant.UserRole;
import com.quanyue.music.domain.entity.FmVip;
import com.quanyue.music.domain.entity.InvitationCode;
import com.quanyue.music.order.service.OrderService;
import com.quanyue.music.order.type.OrderCreateRequest;
import com.quanyue.music.order.type.OrderResult;
import com.quanyue.music.user.mapper.VIPMapper;
import com.shzisg.mybatis.mapper.page.Page;
import com.shzisg.mybatis.mapper.page.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "下单及邀请码")
@RestController
@Scope(role = UserRole.MANAGER)
@RequestMapping("/api")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private VIPMapper vipMapper;

    @ApiOperation(value = "邀请码兑换")
    @Scope(role = UserRole.USER)
    @GetMapping(value = "/order/code/convert", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> convert(@RequestParam("code") String code,
                                           Principal principal,
                                           HttpServletRequest request) {
        return ServiceResult.success(orderService.convert(code, principal.getId(), WebUtils.getClientIp(request)));
    }

    @ApiOperation(value = "生成邀请码")
    @GetMapping(value = "/order/code/create", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <InvitationCode>> createCode(@RequestParam("month") String month,
                                                            @RequestParam("number") String number,
                                                            @RequestParam("isIp") String isIp,
                                                            @RequestParam("remark") String remake) {
        return ServiceResult.success(orderService.createCode(Integer.valueOf(month), Integer.valueOf(number), isIp, remake));
    }

    @ApiOperation(value = "邀请码列表")
    @GetMapping(value = "/order/code/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <InvitationCode>> codeList() {
        return ServiceResult.success(orderService.codeList());
    }

    @ApiOperation(value = "邀请码导出Excel")
    @GetMapping(value = "/code/list/excel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <String> codeExcel() {
        return ServiceResult.success(orderService.codeExcel());
    }

    @ApiOperation(value = "删除邀请码")
    @DeleteMapping(value = "/order/code/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> codeDelete(@RequestParam("code") String code) {
        orderService.codeDelete(code);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "充值")
    @Scope(role = UserRole.USER)
    @PostMapping(value = "/order/create", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <OrderResult> order(@RequestBody OrderCreateRequest create,
                                             @ApiIgnore HttpServletRequest request,
                                             @ApiIgnore RequestContext context) {
        return ServiceResult.success(orderService.createOrder(create, context.getUser(), WebUtils.getClientIp(request)));
    }


    @ApiOperation(value = "VIP列表")
    @PostMapping(value = "/vip/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Page <FmVip>> vipList(@RequestBody PageRequest request) {
        request.setOrderBy("start_time");
        request.setAsc(false);
        return ServiceResult.success(vipMapper.findLimitValid(request));
    }
}
